﻿Imports ATS.Business.Entities
Imports ATS.Data
Imports System.Transactions
Imports System.Collections.Generic
Imports System.Collections.ObjectModel

Public Class DepartmentComponent

#Region "CRUD"
    Public Function CreateDepartment(_DepartmentDetails As Department) As Department
        Return CreateNewDepartment(_DepartmentDetails)
    End Function

    Public Function ReadDepartmentDetails(_searchDepartment As String) As Department
        ' Retrieve data.
        Dim dac As New AssetDataAccess()
        Try
            Return dac.GetDepartment(_searchDepartment)
        Catch ex As Exception
            Console.WriteLine(ex.Message)
            Throw ex
        End Try
    End Function

    Public Function GetAllDepartments() As List(Of Department)
        ' Retrieve data.
        Dim dac As New AssetDataAccess()
        Try
            Return dac.GetAllDepartments()
        Catch ex As Exception
            Console.WriteLine(ex.Message)
            Throw ex
        End Try
    End Function

    Public Function GetDepartmentList() As List(Of String)
        ' Retrieve data.
        Dim dac As New AssetDataAccess()
        Try
            Return dac.GetDepartmentList
        Catch ex As Exception
            Console.WriteLine(ex.Message)
            Throw ex
        End Try
    End Function

    Public Function UpdateDepartment(_DepartmentDetail As Department) As Department
        Return UpdateDepartmentDetails(_DepartmentDetail)
    End Function


    Public Sub DeleteDepartment(_DepartmentDetails As Department)
        Dim dac As New AssetDataAccess
        Try
            dac.DeleteDepartment(_DepartmentDetails)
        Catch ex As Exception
            Console.WriteLine(ex.Message)
            Throw ex
        End Try

    End Sub
#End Region

#Region "Support Procedures"
    Private Function CreateNewDepartment(DepartmentDetails As Department) As Department
        Dim dac As New AssetDataAccess()
        Try
            Return dac.CreateDepartment(DepartmentDetails)
        Catch ex As Exception
            Console.WriteLine(ex.Message)
            Throw ex
        End Try
    End Function

    Private Function UpdateDepartmentDetails(DepartmentDetail As Department) As Department
        Using ts As New TransactionScope(TransactionScopeOption.Required)
            Try
                Dim dac As New AssetDataAccess()
                dac.UpdateDepartmentDetails(DepartmentDetail)
                ts.Complete()
            Catch ex As TransactionAbortedException
                Console.WriteLine("TransactionAbortedException Message: {0}", ex.Message)
                Throw ex
            Catch ex As ApplicationException
                Console.WriteLine("ApplicationException Message: {0}", ex.Message)
                Throw ex
            Catch ex As Exception
                Console.WriteLine(ex.Message)
                Throw ex
            End Try
        End Using
        Return DepartmentDetail
    End Function
#End Region
End Class
